package cn.cpf.app.pane;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

public class TextDirectionConvert extends JFrame {

    private JPanel contentPane;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    TextDirectionConvert frame = new TextDirectionConvert();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the cn.cpf.app.gobang.frame.
     */
    public TextDirectionConvert() {
        super();
        getContentPane().setBackground(Color.GRAY);
        setBackground(Color.LIGHT_GRAY);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setTitle("LengthwaysTextDispose");
        setBounds(100, 100, 380, 500);
        getContentPane().setLayout(null);

        JTextArea ja_source = new JTextArea();
        ja_source.setText("请将所需要转换的文本写在这里\n之后点击文本按钮\n下面的文本框将会显示转换的文本\n若是点击效果按钮\n则会显示转换的效果样式！");
        ja_source.setFont(new Font("新宋体", Font.PLAIN, 12));
        ja_source.setBackground(Color.LIGHT_GRAY);
        final JScrollPane jSPane1 = new JScrollPane(ja_source);
        jSPane1.setLocation(0, 0);
        jSPane1.setSize(364, 160);
        getContentPane().add(jSPane1);

        JTextArea ja_result = new JTextArea();
        ja_result.setText("\u8F6C\n\u6362\n\u540E\n\u6587\n\u672C");
        ja_result.setFont(new Font("新宋体", Font.PLAIN, 12));
        ja_result.setBackground(Color.LIGHT_GRAY);
        final JScrollPane jSPane2 = new JScrollPane(ja_result);
        jSPane2.setLocation(0, 201);
        jSPane2.setSize(364, 260);
        getContentPane().add(jSPane2);

        JButton exeBtn = new JButton("文本代码");
        exeBtn.setLocation(61, 170);
        exeBtn.setSize(90, 24);
        exeBtn.addActionListener(e -> {
            ja_result.setLineWrap(true);                 //激活自动换行功能
            ja_result.setWrapStyleWord(true);            // 激活断行不断字功能
            String sourceText = ja_source.getText();
            String tanslatedText = work(sourceText);
            ja_result.setText(tanslatedText);
        });
        getContentPane().add(exeBtn);

        JButton clrBtn = new JButton("文本效果");
        clrBtn.setLocation(212, 170);
        clrBtn.setSize(90, 24);
        clrBtn.addActionListener(e -> {
            ja_result.setLineWrap(false);                 //激活自动换行功能
            ja_result.setWrapStyleWord(false);            // 激活断行不断字功能
            String sourceText = ja_source.getText();
            String tanslatedText = work(sourceText);
            tanslatedText = tanslatedText.replaceAll("\\\\n", "\n");
            ja_result.setText(tanslatedText);
        });
        getContentPane().add(clrBtn);

        clrBtn.doClick();
    }

    private String work(String st) {
        String str = null;
        StringBuilder build = new StringBuilder();
        String[] astr = st.split("\n");
        int len = astr.length;
        int max = 0;
        for (int i = 0; i < len; i++) {
            if (astr[i].length() > max)
                max = astr[i].length();
        }

        for (int i = 0; i < max; i++) {
            for (int j = 0; j < len; j++) {
                if (astr[j].length() <= i) {
                    if (j == len - 1) ;
                    else build.append("   ");
                } else if (j == len - 1)
                    build.append(astr[j].charAt(i));
                else build.append(astr[j].charAt(i)).append(" ");
            }
            build.append("\\n");
            str = build.toString();
        }
        return str;
    }

}
